package com.fieldchain.ex.fa100.ui.personal.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.User;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.megvii.faceid.zzplatform.sdk.manager.FaceppManager;
import com.megvii.faceid.zzplatform.sdk.manager.MegLiveDetectListener;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;
import com.fieldchain.ex.fa100.utils.third.face.CheckSelfUtils;
import com.fieldchain.ex.fa100.utils.third.face.FaceErrorUtils;

import java.util.HashMap;
import java.util.Random;

public class AuthActivity extends MyBaseActivity implements MegLiveDetectListener {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private LinearLayout linInput;
    private EditText etName;
    private EditText etIdcard;
    private TextView tvSure;
    private LinearLayout linShow;
    private TextView tvName;
    private TextView tvIdcard;
    private String name, idCard;
    private int type = 0;
    private String token;


    private User user;
    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        linInput = findViewById(R.id.lin_input);
        etName = findViewById(R.id.et_name);
        etIdcard = findViewById(R.id.et_idcard);
        tvSure = findViewById(R.id.tv_sure);
        linShow = findViewById(R.id.lin_show);
        tvName = findViewById(R.id.tv_name);
        tvIdcard = findViewById(R.id.tv_idcard);
    }

    @Override
    public void setValue() {
        reaBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        FaceppManager.getInstance(this).setMegLiveDetectListener(this);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                send(1);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        user=ShareManager.getInstance().getUser();
        if(user==null){
            loginShow();
        }else {
            user=ShareManager.getInstance().getUser();
            type=user.getIdCardAuth();
            if (type == 0) {
                tvCenter.setText("实名认证");
                linInput.setVisibility(View.VISIBLE);
                linShow.setVisibility(View.GONE);
            } else {
                User user = ShareManager.getInstance().getUser();
                if (user != null) {
                    idCard=user.getIdCard();
                    String show_id = idCard.substring(0, 5) + "********" + idCard.substring(idCard.length()-5,idCard.length());
                    tvName.setText(user.getTrueName());
                    tvIdcard.setText(show_id);
                    tvCenter.setText("用户信息");
                    linInput.setVisibility(View.GONE);
                    linShow.setVisibility(View.VISIBLE);
                }
            }
        }

    }

    @Override
    public int getLayout() {
        return R.layout.activity_checkself;
    }

    @Override
    public void onCallback(int code, String s) {
        if (code == 1000) {
            send(2);
            String show_id = idCard.substring(0, 5) + "********" + idCard.substring(idCard.length()-5,idCard.length());
            tvName.setText(name);
            tvIdcard.setText(show_id);
            tvCenter.setText("用户信息");
            linInput.setVisibility(View.GONE);
            linShow.setVisibility(View.VISIBLE);


        }
        MyToast.show(new FaceErrorUtils().getMessage(code, s));
    }

    public String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    private void send(final int type) {
        HashMap map=new HashMap();
        name=etName.getText().toString();
        idCard=etIdcard.getText().toString();
        if(type==1){
            token= getRandomString(new Random().nextInt(10)+5);
        }else{
            map.put("name",name);
            map.put("idCard",idCard);
        }
        map.put("token",token);

        HttpRequest.getInstance().postRequest(map, Constant.authorNotify, new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                if(code==200){
                    if(type==1){
                        CheckSelfUtils.start(mContext, name, idCard, tvSure);
                    }else{
                        user.setTrueName(name);
                        user.setIdCard(idCard);
                        user.setIdCardAuth(1);
                        ShareManager.getInstance().saveUser(user);
                        MyToast.show("认证成功");
                        finish();
                    }
                }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }
}
